﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using QL_NOI_TRU.DataLayers;

namespace QL_NOI_TRU
{
    public partial class frmNhapDSGiuongBenh : DevExpress.XtraEditors.XtraForm
    {
        public frmNhapDSGiuongBenh()
        {
            InitializeComponent();
        }

        private void frmNhapDSGiuongBenh_Load(object sender, EventArgs e)
        {
            //do du lieu vao grid
            var kn = new Ketnoi(frmDangnhap.stringConn);
            gridControl1.DataSource = kn.ExcuteQuery("select TEN_KHOA as 'Tên khoa', PHONG_BENH.MAPHONG as 'Mã phòng' ,MAGIUONG as 'Mã giường', TINHTRANG as 'Tình trạng' from KHOA,PHONG_BENH, GIUONG where KHOA.MA_KHOA = PHONG_BENH.MA_KHOA and PHONG_BENH.MAPHONG = GIUONG.MAPHONG");

            // do du lieu vao cbbKhoa
            var a = "select * from KHOA";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = a;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                cbbKhoa.Properties.Items.Add(String.Format("{0} | {1}", dr["MA_KHOA"], dr["TEN_KHOA"]));
            }
            dr.Close();
        }

        private void cbbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            string t = cbbKhoa.Text.Trim();
            string mt = t.Substring(0, 2).Trim();
            var a = "select MAPHONG from PHONG_BENH where MA_KHOA = '" + mt + "'";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = a;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                cbbPhong.Properties.Items.Add(String.Format("{0}", dr["MAPHONG"]));
            }

        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            string a;
            if (radioButton1.Checked == true)
                a = radioButton1.Text.Trim();
            else
                a = radioButton2.Text.Trim();
            string strr = "EXEC Insert_GIUONG @MAPHONG = '" + cbbPhong.Text.Trim() + "',@MAGIUONG = '" + txtMaGiuong.Text.Trim() + "',@TINHTRANG = N'"+a+"'";
            var kn = new Ketnoi(frmDangnhap.stringConn);
            kn.ExcuteQuery(strr);
            frmNhapDSGiuongBenh_Load(null, null);
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            cbbKhoa.SelectedIndex= -1;
            cbbPhong.SelectedIndex = -1;
            txtMaGiuong.Text = "";
         //   radioButton1.Checked = false;
         //   radioButton2.Checked = false;
        }
    }
}